package com.jackrain.nea.fc.ps.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.common.PostDubbleEvent;
import com.jackrain.nea.common.PostHandleEvent;
import com.jackrain.nea.common.QueryUserUtil;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.WmsSupplierAndSkuCmd;
import com.jackrain.nea.fc.utils.UserInformation;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySessionImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
/**
 * 维护商品档案
 * @author zhang.zz
 * @date 2018/11/28
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ps")
public class WmsSupplierAndSkuCmdImpl extends CommandAdapter implements WmsSupplierAndSkuCmd {
    @Autowired
    PostDubbleEvent postDubbleEvent;
    @Autowired
    PostHandleEvent postHandleEvent;

    @Autowired
    QueryUserUtil postUserEvent;
    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
//        log.info("WmsSupplierAndSkuCmd---begin--->");
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),
                "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("WmsSupplierAndSkuCmdImpl--param-->{}",param);

        if (null == param || param.size() == 0) {
            throw new NDSException("参数格式错误！");
        }
        User user = postUserEvent.getUser(UserInformation.USER_NAME);
        QuerySessionImpl querySession = new QuerySessionImpl(user);
        DefaultWebEvent event = new DefaultWebEvent("dlRetail",
                new HashMap(16));

        String noticSave = "com.jackrain.nea.ps.api.CproSupplierCmd:1.0:ps";
        event.put("param", param);
        querySession.setEvent(event);
        //调用标准服务
        try {
            JSONObject retAddJo = postDubbleEvent.executePostDubble(noticSave, querySession);
            log.info("WmsSupplierAndSkuCmd---->{}",retAddJo);
            if (retAddJo != null && retAddJo.size() > 0) {
                JSONArray dataArray = retAddJo.getJSONArray("data");
                if (dataArray == null || dataArray.size() <= 0) {
                    throw new NDSException("服务端没有数据，请求参数" + param.toJSONString());
                }
                vh.put("data",dataArray);

            } else {
                throw new NDSException("服务端没有数据，请求参数" + retAddJo.toJSONString());
            }
        } catch (Exception e) {
            log.error("WmsSupplierAndSkuCmd--Error-->"+e);
            throw new NDSException("调用标服错误" + e);
        }

        return vh;
    }
}
